<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <link href="../../component/pear/css/pear.css" rel="stylesheet"/>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <div class="layui-btn-group">
            <button type="button" class="layui-btn" id="check">校验</button>
            <button type="button" class="layui-btn" id="start">启动</button>
            <button type="button" class="layui-btn" id="stop">停止</button>
            <button type="button" class="layui-btn" id="reload">热启动</button>
        </div>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="" lay-filter="nginx">

            <div class="layui-form-item" style="width: 100%;">
                <label class="layui-form-label">配置路径</label>
                <div class="layui-input-inline" style="width: 36%;">
                    <input type="text" name="path" id="nginxConfPath" autocomplete="off" class="layui-input"
                           style="width: 99%;"
                           placeholder="请输入nginx配置文件路径" lay-verify="title">
                </div>
                <button type="button" class="layui-btn" id="getConf">加载</button>
                <button type="submit" class="layui-btn" lay-submit="" lay-filter="save">保存</button>
            </div>
            <div class="layui-form-item adminj-sort-item" id="did_1629876782412_1" cpt_id="textarea"
                 style="width: 100%;">
                <label class="layui-form-label"></label>
                <div class="layui-input-inline" style="width: 66%;">
                    <textarea id="nginxContent" name="context" class="layui-textarea"
                              style="width: 99%; height: 600px"></textarea>
                </div>

            </div>
        </form>
    </div>
</div>


</body>

<script src="../../component/layui/layui.js"></script>
<script src="../../component/pear/pear.js"></script>
<script>
    layui.use(['easyAdmin', 'form'], function () {
        let easyAdmin = layui.easyAdmin;
        var $ = layui.$; //使用jQuery 重点处
        let form = layui.form;
        getConfContent();
        $("#getConf").click(function () {
            getConfContent();
        });

        $("#check").click(function () {
            easyAdmin.http({
                url: "/sys/nginx/check",
                data: JSON.stringify(form.val("nginx")),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (result) {
                    if (result.success) {
                        layer.msg(result.msg,
                            {
                                icon: 1,
                                time: 2000,
                                area: ['100px', '65px'],
                                content: "执行成功"
                            });
                    } else {
                        layer.msg(result.msg,
                            {
                                icon: 2,
                                time: 2000,
                                area: ['250px', '70px']
                            }
                        );
                    }
                }
            })
        });
        $("#start").click(function () {
            easyAdmin.http({
                url: "/sys/nginx/start",
                data: JSON.stringify(form.val("nginx")),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (result) {
                    if (result.success) {
                        layer.msg(result.msg,
                            {
                                icon: 1,
                                time: 2000,
                                area: ['100px', '65px'],
                                content: "执行成功"
                            });
                    } else {
                        layer.msg(result.msg,
                            {
                                icon: 2,
                                time: 2000,
                                area: ['250px', '70px']
                            }
                        );
                    }
                }
            })
        });
        $("#stop").click(function () {
            easyAdmin.http({
                url: "/sys/nginx/stop",
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (result) {
                    if (result.success) {
                        layer.msg(result.msg,
                            {
                                icon: 1,
                                time: 2000,
                                area: ['100px', '65px'],
                                content: "执行成功"
                            });
                    } else {
                        layer.msg(result.msg,
                            {
                                icon: 2,
                                time: 2000,
                                area: ['250px', '70px']
                            }
                        );
                    }
                }
            })
        });
        $("#reload").click(function () {
            easyAdmin.http({
                url: "/sys/nginx/reload",
                data: JSON.stringify(form.val("nginx")),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (result) {
                    if (result.success) {
                        layer.msg(result.msg,
                            {
                                icon: 1,
                                time: 2000,
                                area: ['100px', '65px'],
                                content: "执行成功"
                            });
                    } else {
                        layer.msg(result.msg,
                            {
                                icon: 2,
                                time: 2000,
                                area: ['250px', '70px']
                            }
                        );
                    }
                }
            })
        });
        form.on('submit(save)', function (data) {
            // 转换
            easyAdmin.http({
                url: "/sys/nginx",
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (result) {
                    if (result.success) {
                        layer.msg(result.msg,
                            {
                                icon: 1,
                                time: 2000,
                                area: ['100px', '65px'],
                                content: "保存成功"
                            }, function () {
                                getConfContent();
                            });
                    } else {
                        layer.msg(result.msg,
                            {
                                icon: 2,
                                time: 2000,
                                area: ['250px', '70px']
                            }
                        );
                    }
                }
            })
            return false;
        });

        function getConfContent() {
            var nginxConfPath = $('#nginxConfPath').val();
            easyAdmin.httpGet("/sys/nginx?path=" + nginxConfPath, function (res) {
                if (res.success) {
                    $('#nginxContent').val(res.data);
                } else {
                    layer.msg(res.msg,
                        {icon: 2, time: 2000, area: ['230px', '65px']}
                    );
                }
            });
        }

    })
</script>
</html>
